package com.givemepass.FragmentTabs;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class Fragment2Tab1CreateActivity extends Fragment {
	private View v;
	private Spinner spinner;
	private EditText actName, company, actContent, deadline, actTime, actAddr, tel, mail, number, condition;
    private ArrayAdapter<String> listAdapter;
    private Context mContext;
    private String account1, con, ma, na, de, te, com, ti, co, ad, nu;
    
    private String account;
	int classifyInt;
	
	RadioGroup radiogroup1;
	RadioButton limit;
	RadioButton noLimit;
	String gender;
   
    @Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);	
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		v = inflater.inflate(R.layout.fragment2_tab1_create_activity, container, false);
		
		account = getArguments().getString("account");
		
		actName = (EditText)v.findViewById(R.id.actName);
		company = (EditText)v.findViewById(R.id.name);
		actContent = (EditText)v.findViewById(R.id.actCon);
		deadline = (EditText)v.findViewById(R.id.deadline);
		actTime = (EditText)v.findViewById(R.id.actTime);
		actAddr = (EditText)v.findViewById(R.id.actAddr);
		tel = (EditText)v.findViewById(R.id.tel);
		mail = (EditText)v.findViewById(R.id.mail);
		number = (EditText)v.findViewById(R.id.number);
		condition = (EditText)v.findViewById(R.id.condition);
		
		radiogroup1 = (RadioGroup)v.findViewById(R.id.rdg1);
		limit = (RadioButton)v.findViewById (R.id.radioLimit);
		noLimit = (RadioButton)v.findViewById (R.id.radioNoLimit);	
		radiogroup1.setOnCheckedChangeListener(new GenderOnCheckedChangeListener(){
			
		});
		
		String [] values = {"學術研討","運動","藝術人文","休閒娛樂","公益","音樂","其他"};
		Spinner spinner = (Spinner) v.findViewById(R.id.spinner1);
		ArrayAdapter<String> LTRadapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values);
		LTRadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		spinner.setAdapter(LTRadapter);
		spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView adapterView, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity().getApplicationContext(), "活動分類:【"+adapterView.getSelectedItem().toString()+"】",Toast.LENGTH_LONG).show();
				classifyInt = position+1;
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity().getApplicationContext(), "尚未選擇活動分類!",Toast.LENGTH_LONG).show();
			}
			
		});
		
		Button bt1 = (Button) v.findViewById(R.id.btnSend);
        ((Button) bt1).setOnClickListener(new OnClickListener() {
        	@Override
			public void onClick(View v) {          
				// TODO Auto-generated method stub
        		na = actName.getText().toString();
        		com = company.getText().toString();
        		con = actContent.getText().toString();
        		de = deadline.getText().toString();
        		ti = actTime.getText().toString();
        		ad = actAddr.getText().toString();
        		te = tel.getText().toString();
        		ma = mail.getText().toString();
        		nu = number.getText().toString();
        		co = condition.getText().toString();
        		
        		if (co.equals("") || ad.equals("") || con.equals("") || ma.equals("") 
        				|| na.equals("") || nu.equals("") || com.equals("")  
        				|| te.equals("") || de.equals("") || ti.equals("")){       		
        			Toast.makeText(getActivity().getApplicationContext(), "有資料未輸入喔!",Toast.LENGTH_LONG).show();
        		}
        		
        		else{
        			Bundle args = new Bundle();
        			
        			if( conDB(account.toString().trim(),Integer.toString(classifyInt),
        					actName.getText().toString().trim(),company.getText().toString().trim(), actContent.getText().toString().trim(), deadline.getText().toString().trim(), actTime.getText().toString().trim(), actAddr.getText().toString().trim(), tel.getText().toString().trim(), mail.getText().toString().trim(), number.getText().toString().trim(), condition.getText().toString().trim() ) ) {
					
        				Toast.makeText(getActivity().getApplicationContext(), "創建活動成功",Toast.LENGTH_LONG).show();
        				
        				Fragment newFragment = new Fragment1ViewActivity();
 		     	       
        				FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        				newFragment.setArguments(args);
		        	   
        				ft.replace(R.id.realtabcontent, newFragment);
        				ft.setTransition(FragmentTransaction.TRANSIT_NONE);
        				ft.addToBackStack(null);
        				ft.commit();
        			}
        			else {
        				Toast.makeText(getActivity().getApplicationContext(), "創建活動失敗，請重新輸入",Toast.LENGTH_LONG).show();
        			}
        		}
			}	
        });
		return v;
	}
	
	private class GenderOnCheckedChangeListener implements OnCheckedChangeListener{  
        @Override  
        public void onCheckedChanged(RadioGroup group,int checkedId){  
        	if(limit.getId()==checkedId){
        		Toast.makeText(getActivity().getApplicationContext(), "請輸入人數",Toast.LENGTH_LONG).show();
    		}else if(noLimit.getId()==checkedId){
    			}
        }  
    }	
	
	//與本機連線
		private boolean conDB(String account, String classify,
				String aN, String host, String i, String d, String time, String p, String t, String e, String n, String c) 
		{
			String strRet = "q";
		    
		    try{
		    	HttpClient httpclient = new DefaultHttpClient();
		    	HttpPost httpost = new HttpPost("http://163.21.245.199/AndroidConnectDB/android_createact.php");
		    	
		    	ArrayList <NameValuePair> nvps = new ArrayList <NameValuePair>();
		        nvps.add(new BasicNameValuePair("account", account));
		        nvps.add(new BasicNameValuePair("classify", classify));
		        nvps.add(new BasicNameValuePair("act_name", aN));
		        nvps.add(new BasicNameValuePair("host", host));
		        nvps.add(new BasicNameValuePair("introduction", i));
		        nvps.add(new BasicNameValuePair("deadline", d));
		        nvps.add(new BasicNameValuePair("time", time));
		        nvps.add(new BasicNameValuePair("place", p));
		        nvps.add(new BasicNameValuePair("host_tel", t));
		        nvps.add(new BasicNameValuePair("host_email", e));
		        nvps.add(new BasicNameValuePair("amount", n));
		        nvps.add(new BasicNameValuePair("condition", c));
		        
		        httpost.setEntity(new UrlEncodedFormEntity(nvps,"UTF-8"));
		        
		        ResponseHandler<String> responseHandler = new BasicResponseHandler();
		        final String res = httpclient.execute(httpost, responseHandler);		   
		        
		        Log.i("CREATE", account);
		        
		        if(!res.equalsIgnoreCase(strRet)) {
		        	Log.i("CREATE", "YES");
		        	return true;
		        }
		        else {
		        	Log.i("CREATE", "NO"+res);
		        	return false;
		        }
		    }catch(Exception ee) {
		    	ee.printStackTrace();
		        return false;
		    }
		}
	
}
